#ifndef _CONMMUNICATION_H_
#define _CONMMUNICATION_H_

#define COMMUNICATION_HEAD_1    (0xAA)
#define COMMUNICATION_HEAD_2    (0x55)

#define PACKAGE_LENGTH              (20)

/* 顶层协议部分数据的长度 */
#define TOP_PROTOCOL_NUM            (20)
#define TOP_PROTOCOL_DATA_LENGTH    (1)
#define TOP_CMD_TYPE_LENGTH         (1)
#define TOP_CMD_LENGTH              (1)
#define TOP_ACK_LENGTH              (1)

/* 底层协议部分数据的长度 */
#define DEV_COMMUNICATION_HEAD_LENGTH       (2)
#define DEV_EFFECTIVE_DATA_LENGTH_LENGTH    (2)
#define DEV_EFFECTIVE_DATA_LENGTH           (15)
#define DEV_CHECK_LENGTH                    (1)

/* 顶层协议的请求包和应答包的命令类别 */
#define REQUEST_ANSWER_BAG      (0xC0)

/* 顶层协议的包的命令码 */
/* 登录 */
#define CODE_LOGIN_REQUEST           (0x01)
#define CODE_LOGIN_REQUEST_ACK       (0x02)
#define CODE_LOGIN_REQUEST_ERROR     (0x03)
#define CODE_LOGIN_ANSWER            (0x04)
#define CODE_LOGIN_ANSWER_ACK        (0x05)
#define CODE_LOGIN_ANSWER_ERROR      (0x06)
/* 注册 */
#define CODE_REGISTER_REQUEST        (0x07)
#define CODE_REGISTER_REQUEST_ACK    (0x08)
#define CODE_REGISTER_REQUEST_ERROR  (0x09)
#define CODE_REGISTER_ANSWER         (0x0A)
#define CODE_REGISTER_ANSWER_ACK     (0x0B)
#define CODE_REGISTER_ANSWER_ERROR   (0x0C)
/* 增加好友 */
#define CODE_ADD_FRIEND_REQUEST          (0x0D)
#define CODE_ADD_FRIEND_REQUEST_ACK      (0x0E)
#define CODE_ADD_FRIEND_REQUEST_ERROR    (0x0F)
#define CODE_ADD_FRIEND_ANSWER           (0x10)
#define CODE_ADD_FRIEND_ANSWER_ACK       (0x11)
#define CODE_ADD_FRIEND_ANSWER_ERROR     (0x12)
/* 删除好友 */
#define CODE_DELETE_FRIEND_REQUEST       (0x13)
#define CODE_DELETE_FRIEND_REQUEST_ACK   (0x14)
#define CODE_DELETE_FRIEND_REQUEST_ERROR (0x15)
#define CODE_DELETE_FRIEND_ANSWER        (0x16)
#define CODE_DELETE_FRIEND_ANSWER_ACK    (0x17)
#define CODE_DELETE_FRIEND_ANSWER_ERROR  (0x18)
/* 发送消息 */
#define CODE_SEND_MESSAGE_REQUEST        (0x19)
#define CODE_SEND_MESSAGE_REQUEST_ACK    (0x1A)
#define CODE_SEND_MESSAGE_REQUEST_ERROR  (0x1B)
#define CODE_SEND_MESSAGE_ANSWER         (0x1C)
#define CODE_SEND_MESSAGE_ANSWER_ACK     (0x1D)
#define CODE_SEND_MESSAGE_ANSWER_ERROR   (0x1E)

/* 顶层协议的包 */
enum top_protocol_cmd {
    LOGIN_REQUEST = 0,
    LOGIN_REQUEST_ACK,
    LOGIN_REQUEST_ERROR,
    LOGIN_ANSWER,
    LOGIN_ANSWER_ACK,
    LOGIN_ANSWER_ERROR,

    REGISTER_REQUEST,
    REGISTER_REQUEST_ACK,
    REGISTER_REQUEST_ERROR,
    REGISTER_ANSWER,
    REGISTER_ANSWER_ACK,
    REGISTER_ANSWER_ERROR,

    ADD_FRIEND_REQUEST,
    ADD_FRIEND_REQUEST_ACK,
    ADD_FRIEND_REQUEST_ERROR,
    ADD_FRIEND_ANSWER,
    ADD_FRIEND_ANSWER_ACK,
    ADD_FRIEND_ANSWER_ERROR,

    DELETE_FRIEND_REQUEST,
    DELETE_FRIEND_REQUEST_ACK,
    DELETE_FRIEND_REQUEST_ERROR,
    DELETE_FRIEND_ANSWER,
    DELETE_FRIEND_ANSWER_ACK,
    DELETE_FRIEND_ANSWER_ERROR,

    SEND_MESSAGE_REQUEST,
    SEND_MESSAGE_REQUEST_ACK,
    SEND_MESSAGE_REQUEST_ERROR,
    SEND_MESSAGE_ANSWER,
    SEND_MESSAGE_ANSWER_ACK,
    SEND_MESSAGE_ANSWER_ERROR
};

/* 底层协议数据帧 */
struct device_protocol {
    char head[DEV_COMMUNICATION_HEAD_LENGTH];
    char effective_data_length[DEV_EFFECTIVE_DATA_LENGTH_LENGTH];
    char effective_data[DEV_EFFECTIVE_DATA_LENGTH];
    char check[DEV_CHECK_LENGTH];
};

/* 顶层协议 */
struct top_protocol {
    char cmd_type[TOP_CMD_TYPE_LENGTH];
    char cmd[TOP_CMD_LENGTH];
    char ack[TOP_ACK_LENGTH];
    char data[TOP_PROTOCOL_DATA_LENGTH];
};

/* 一个数据包的内容和fd描述符 */
struct data_package {
    int fd;
    char data[PACKAGE_LENGTH];
};

struct communication_option {
    int (*communication_login)(char *);
    int (*communication_register)(char *);
    int (*communication_add_friend)(char *);
    int (*communication_delete_friend)(char *);
    int (*communication_send_message)(char *);
};

struct communication_core {
    struct data_package *data_package;
    struct top_protocol top_protocol[TOP_PROTOCOL_NUM];
    struct device_protocol *device_protocol;
    struct communication_option *option;
};

extern struct communication_core *communication_init(void);
extern int communication_analysis(char *data);
extern char *communication_top_build(struct communication_core *communication_core);
extern char *communication_device_build(struct communication_core *communication_core);
extern int communication_top_analysis(char *data);
extern char *communication_dev_analysis(char *data);


#endif